package com.game.model;

import com.game.controller.GoodsController;
import com.game.data.BagData;
import com.game.model.goods.Goods;

import java.io.Serializable;
import java.util.*;

/**
 * @Description:背包
 **/
public class Bag implements Serializable {
    private Map<Goods, Integer> goodsMap;//物品列表 （物品+数量）
    private int bagSize;//背包大小

    public Map<Goods, Integer> getGoodsMap() {
        return goodsMap;
    }

    public void setGoodsMap(Map<Goods, Integer> goodsMap) {
        this.goodsMap = goodsMap;
    }

    public int getBagSize() {
        return bagSize;
    }

    public void setBagSize(int bagSize) {
        this.bagSize = bagSize;
    }

    public Bag() {
    }

    public Bag(Map<Goods, Integer> goodsMap, int bagSize) {
        this.goodsMap = goodsMap;
        this.bagSize = bagSize;
    }
    //添加物品到背包(重载)
    public boolean addToBag(Goods goods) {
        Map<Goods, Integer> map = new HashMap<>();
        map.put(goods, 1);
        return addToBag(map);
    }
    //添加物品到背包(重载)
    public boolean addToBag(Goods goods,Integer count) {
        Map<Goods, Integer> map = new HashMap<>();
        map.put(goods, count);
        return addToBag(map);
    }
    //添加物品到背包
    public boolean addToBag(Map<Goods, Integer> goodsMap) {
        if (goodsMap == null) return true;
        boolean flag = false;
        try {
            Iterator<Map.Entry<Goods, Integer>> iterator = goodsMap.entrySet().iterator();
            Iterator<Map.Entry<Goods, Integer>> iterator1 = this.goodsMap.entrySet().iterator();
            while (iterator.hasNext()) {
                flag =false;
                Map.Entry<Goods, Integer> next = iterator.next();
                //如果是经验或金钱类的就直接使用
                if (next.getKey().getId() == 1 || next.getKey().getId() == 2) {
                    GoodsController.useGoods(next.getKey(), next.getValue());
                    continue;
                }
                while (iterator1.hasNext()) {
                    Map.Entry<Goods, Integer> bagNext = iterator1.next();
                    //如果背包里已经有了就数量相加
                    if (next.getKey().equals(bagNext.getKey())) {
                        bagNext.setValue(bagNext.getValue() + next.getValue());
                        //添加完毕，防止再添加
                        flag = true;
                        break;
                    }
                }
                if (flag) continue;//进入下一次遍历
                //背包满了
                if (goodsMap.size()>=bagSize){
                    System.out.println("！！！！！！背包满了无法获取物品！！！！！");
                    return false;
                }
                //添加入背包
                this.goodsMap.put(next.getKey(), next.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    //从背包移除物品（重载）
    public boolean removeToBag(Goods goods){
        Map<Goods, Integer> map = new HashMap<>();
        map.put(goods, 1);
        return removeToBag(map);
    }
    //从背包移除物品（重载）
    public boolean removeToBag(Goods goods,Integer count){
        Map<Goods, Integer> map = new HashMap<>();
        map.put(goods, count);
        return removeToBag(map);
    }
    //从背包移除物品
    public boolean removeToBag(Map<Goods,Integer>goodsMap){
        if (goodsMap==null)return true;
        try {
            Iterator<Map.Entry<Goods, Integer>> iterator = goodsMap.entrySet().iterator();
            Iterator<Map.Entry<Goods, Integer>> iterator1 = this.goodsMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<Goods, Integer> next = iterator.next();
                while (iterator1.hasNext()){
                    Map.Entry<Goods, Integer> bagNext = iterator1.next();
                    //如果背包存在要删除的物品
                    if (next.getKey().equals(bagNext.getKey())){
                        //如果要删除的数量小于背包原来的数量就相减
                        if(next.getValue()<bagNext.getValue()){
                            bagNext.setValue(bagNext.getValue()-next.getValue());
                        }else {
                            //其他情况下直接从背包移除
                            if (BagData.bag.getGoodsMap().size()>0){
                                BagData.bag.getGoodsMap().remove(bagNext.getKey());
                            }
                        }
                    }
                }
            }
        }catch (ConcurrentModificationException ce){
            //批量删除时暂时忽略一下并发越界问题
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
    @Override
    public String toString() {
        return "Bag{" +
                "goodsMap=" + goodsMap +
                ", bagSize=" + bagSize +
                '}';
    }
}
